package com.cskaoyan.market.controller;/*
 *文件名: GeneralProblemController
 *创建者:wxin
 *创建时间:2024/5/6 14:52
 */

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.AdminIssueService;
import com.cskaoyan.market.service.AdminIssueServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/issue/*")
public class AdminIssueController extends HttpServlet {

    private AdminIssueService issueService = new AdminIssueServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");
        if ("create".equals(op)) {
            create(req, resp);
        } else if ("update".equals(op)) {
            update(req, resp);
        } else if ("delete".equals(op)) {
            delete(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        System.out.println(requestBody);
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        if(id==null){
            Object fail = ResponseUtil.fail();
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        int result = issueService.logicalDelete(id);
        if(result==0){
            Object fail = ResponseUtil.fail();
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        Object ok = ResponseUtil.ok();
        String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(jsonStr);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        if (StringUtils.isEmpty(question)) {
            Object fail = ResponseUtil.fail(404, "问题不能为空");
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
            return;
        }
        if (StringUtils.isEmpty(answer)) {
            Object fail = ResponseUtil.fail(404, "问题不能为空");
            String jsonStr = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(jsonStr);
        }
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketIssue marketIssue = objectMapper.readValue(requestBody, MarketIssue.class);
        MarketIssue resultmarketIssue = issueService.updateIssue(marketIssue);
        Map<String , Object> data = new HashMap<>();
        data.put("id",resultmarketIssue.getId());
        data.put("question", resultmarketIssue.getQuestion());
        data.put("answer", resultmarketIssue.getAnswer());
        data.put("addTime", resultmarketIssue.getAddTime());
        data.put("updateTime", resultmarketIssue.getUpdateTime());
        data.put("deleted",resultmarketIssue.getDeleted());
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }


    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        if (StringUtils.isEmpty(question)) {
            //System.out.println("empty");
            Object object = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(object));
        }
        if (StringUtils.isEmpty(answer)) {
            //System.out.println("empty");
            Object object = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(object));
        }
        //Jackson工具类，将json字符串转java对象
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketIssue marketIssue = objectMapper.readValue(requestBody, MarketIssue.class);
        MarketIssue resultmarketIssue = issueService.addIssue(marketIssue);
        HashMap<String, Object> data = new HashMap<>();
        data.put("addTime", resultmarketIssue.getAddTime());
        data.put("answer", resultmarketIssue.getAnswer());
        data.put("id", resultmarketIssue.getId());
        data.put("question", resultmarketIssue.getQuestion());
        data.put("updateTime", resultmarketIssue.getUpdateTime());
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String question = req.getParameter("question");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);

        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(object);
            resp.getWriter().println(s);
            return;
        }
        List<MarketIssue> adminIssueList = issueService.list(limit, page, question, sort, order);

        Object o = ResponseUtil.okList(adminIssueList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }


}
